package de.yellowshoes.tedx.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.util.Log;
import de.yellowshoes.tedx.TEDxConstants;
import de.yellowshoes.tedx.net.exception.CsvReaderException;

public class CsvRemoteReader
    {
    private String  urlText    = null;
    private String  delimiter  = ";";
    private Integer arrayCount = null;

    public CsvRemoteReader( String url )
        {
        this.urlText = url;
        }

    public CsvRemoteReader( String url, String delimiter )
        {
        this.urlText = url;
        this.delimiter = delimiter;
        }

    public CsvRemoteReader( String url, String delimiter, Integer arrayCount )
        {
        this.urlText = url;
        this.delimiter = delimiter;
        this.arrayCount = arrayCount;
        }

    public List< Object[] > read() throws CsvReaderException
        {
        List< Object[] > objectList = new ArrayList< Object[] >();
        URL url = null;
        HttpURLConnection urlConnection = null;

        try
            {
            url = new URL( urlText );
            }
        catch( MalformedURLException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while parsing url " + urlText, e );
            throw new CsvReaderException( e );
            }

        try
            {
            urlConnection = ( HttpURLConnection )url.openConnection();
            }
        catch( IOException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while opening url " + urlText, e );
            throw new CsvReaderException( e );
            }

        // printLastModified( urlConnection );
        printHeader( urlConnection );

        LineNumberReader in = null;
        try
            {
            in = new LineNumberReader( new InputStreamReader( new BufferedInputStream( urlConnection.getInputStream() ) ) );
            }
        catch( IOException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while getting inputstream for url " + urlText, e );
            throw new CsvReaderException( e );
            }

        readStreamIntoList( in, objectList );
        return objectList;
        }

    private void printHeader( HttpURLConnection urlConnection )
        {
        Log.i( TEDxConstants.LOG_TAG, "Header" );
        Map< String, List< String > > fields = urlConnection.getHeaderFields();
        for( Map.Entry< String, List< String > > field : fields.entrySet() )
            {
            String name = field.getKey();
            List< String > values = field.getValue();

            Log.i( TEDxConstants.LOG_TAG, name + ":" );
            for( String string : values )
                {
                Log.i( TEDxConstants.LOG_TAG, string );
                }
            }
        }

    private void printLastModified( HttpURLConnection urlConnection )
        {
        Date date = new Date( urlConnection.getLastModified() );
        DateFormat formater = SimpleDateFormat.getDateTimeInstance();
        Log.i( TEDxConstants.LOG_TAG, "LastModified: " + formater.format( date ) );
        }

    private void readStreamIntoList( LineNumberReader in, List< Object[] > objectList ) throws CsvReaderException
        {
        String line = null;

        try
            {
            while( ( line = in.readLine() ) != null )
                {
                String[] lineTokens = line.split( delimiter );

                if( arrayCount != null )
                    {
                    if( lineTokens.length < arrayCount )
                        {
                        int count = arrayCount - lineTokens.length;
                        List< String > stringList = new ArrayList< String >( Arrays.asList( lineTokens ) );
                        for( ; count > 0; count-- )
                            {
                            stringList.add( "" );
                            }

                        lineTokens = ( String[] )stringList.toArray( new String[ stringList.size() ] );
                        }
                    }
                objectList.add( lineTokens );
                }
            }
        catch( IOException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Error while reading line", e );
            throw new CsvReaderException( e );
            }
        }
    }
